package com.ikas.ai.server.kafka;

import com.ikas.ai.enums.redis.RedisKeyEnum;
import com.ikas.ai.server.module.data.model.DataMete;
import com.ikas.ai.server.module.data.model.TripMete;
import com.ikas.ai.utils.CollectionUtil;
import com.ikas.ai.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hu.bowei@ikasinfo.com
 * @date 2023-5-5 17:06:58
 * @Description 项目启动完成后加载工况数据到redis中
 */
@Component
@Slf4j
public class RedisDataOperation {


    @Autowired
    private RedisUtil redisUtil;


    public Boolean existsWorkFlowStartMete(String machine, String wfName, String meteCode) {
        String key = RedisKeyEnum.START_WORKFLOW_STRING_KEY.key(machine, wfName);
        List<String> machineMeteList = redisUtil.getArrBean(key, String.class);
        if (CollectionUtil.isEmpty(machineMeteList)) {
            return false;
        } else {
            return machineMeteList.contains(meteCode);
        }
    }

    public List<DataMete> getCacheList(String machine, String meteCode) {
        List<DataMete> meteList = new ArrayList<>();
        String key = RedisKeyEnum.ALL_DATA_METE.key(machine);
        List<DataMete> machineMeteList = redisUtil.getArrBean(key, DataMete.class);
        if (CollectionUtil.isEmpty(machineMeteList)) {
            return meteList;
        }
        return machineMeteList.stream().filter(p -> meteCode.equalsIgnoreCase(p.getMeteCode())).collect(Collectors.toList());
    }

    public TripMete getCacheTripMete(String machine, String meteCode) {
        List<TripMete> machineMeteList = redisUtil.getArrBean(RedisKeyEnum.ALL_DATA_TRIP_METE.key(machine), TripMete.class);
        if (CollectionUtil.isEmpty(machineMeteList)) {
            log.error("--->跳机信号未从数据库配置中获取到");
            return null;
        }
        return machineMeteList.stream().filter(p -> meteCode.equalsIgnoreCase(p.getMeteCode())).findFirst().orElse(null);
    }


    public void catchEarlyMeteCode(DataMete mete) {
        String key = RedisKeyEnum.EARLY_CACHE_METE_KEY.key(mete.getMachineNo());
        List<DataMete> machineMeteList = redisUtil.getArrBean(key, DataMete.class);
        if (CollectionUtil.isEmpty(machineMeteList)) {
            machineMeteList = new ArrayList<>();
        }
        machineMeteList.removeIf(p -> p.getId().equals(mete.getId()));
        machineMeteList.add(mete);
        redisUtil.saveArrBean(key, machineMeteList);
    }

    public List<DataMete> getCacheEarlyMeteCode(String machineNo) {
        String key = RedisKeyEnum.EARLY_CACHE_METE_KEY.key(machineNo);
        List<DataMete> list = redisUtil.getArrBean(key, DataMete.class);
        return CollectionUtil.isEmpty(list) ? new ArrayList<>() : list;
    }

    public void removeEarlyMeteCode(String machineNo) {
        String key = RedisKeyEnum.EARLY_CACHE_METE_KEY.key(machineNo);
        log.info("清除缓存:{}", key);
        redisUtil.deleteKey(key);
    }

    public void catchStartMeteCode(DataMete mete) {
        String key = RedisKeyEnum.START_WORKFLOW_OBJECT_KEY.key(mete.getMachineNo());
        List<DataMete> machineMeteList = redisUtil.getArrBean(key, DataMete.class);
        if (CollectionUtil.isEmpty(machineMeteList)) {
            machineMeteList = new ArrayList<>();
        }
        machineMeteList.removeIf(p -> p.getId().equals(mete.getId()));
        machineMeteList.add(mete);
        log.info("添加启动项缓存成功:{}", machineMeteList);
        //缓存5分钟
        redisUtil.saveArrBean(key, machineMeteList, 5 * 60);
    }


    public List<DataMete> getCatchStartMeteCode(String machineNo) {
        String key = RedisKeyEnum.START_WORKFLOW_OBJECT_KEY.key(machineNo);
        List<DataMete> list = redisUtil.getArrBean(key, DataMete.class);
        return CollectionUtil.isEmpty(list) ? new ArrayList<>() : list;
    }

    public void removeStartMeteCode(String machineNo) {
        String key = RedisKeyEnum.START_WORKFLOW_OBJECT_KEY.key(machineNo);
        log.info("清除缓存:{}", key);
        redisUtil.deleteKey(key);
    }


    public void cacheFlowStep(DataMete cacheMete) {
        String key = RedisKeyEnum.WORKFLOW_STEP_KEY.key(cacheMete.getMachineNo());
        List<DataMete> machineMeteList = redisUtil.getArrBean(key, DataMete.class);
        if (CollectionUtil.isEmpty(machineMeteList)) {
            machineMeteList = new ArrayList<>();
        }
        machineMeteList.removeIf(p -> p.getId().equals(cacheMete.getId()));
        machineMeteList.add(cacheMete);
        redisUtil.saveArrBean(key, machineMeteList);
        log.info("添加步骤缓存成功:{}", machineMeteList);
    }

    public List<DataMete> getCacheFlowStep(String machine, String wfName) {
        String key = RedisKeyEnum.WORKFLOW_STEP_KEY.key(machine);
        List<DataMete> dataMeteList = redisUtil.getArrBean(key, DataMete.class);
        if (CollectionUtil.isEmpty(dataMeteList)) {
            dataMeteList = new ArrayList<>();
        }
        return dataMeteList.stream().filter(p -> p.getWfName().equals(wfName)).collect(Collectors.toList());
    }

    public void removeFlowStep(String machine) {
        String key = RedisKeyEnum.WORKFLOW_STEP_KEY.key(machine);
        log.info("清除缓存:{}", key);
        redisUtil.deleteKey(key);
    }


}
